import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { BundleManager } from 'db://assets/Scripts/Framework/Managers/BundleManager';
import { GamerUI } from './GamerUI';
import { MainUI } from './MainUI';
const { ccclass, property } = _decorator;

@ccclass('SkinItemUI')
export class SkinItemUI extends Component {

    @property(Sprite)
    rareSprite: Sprite;

    @property(Sprite)
    itemSprite: Sprite;

    @property(Label)
    nameLabel: Label;

    @property(Label)
    pieceLabel: Label;

    @property(Node)
    choose: Node;

    @property(Node)
    use: Node;

    @property(SpriteFrame)
    emptySf: SpriteFrame;

    @property([SpriteFrame])
    rareSfs: SpriteFrame[] = [];

    rare = 0;
    prefabUrl: string;
    data: any;

    start() {

    }

    update(deltaTime: number) {

    }

    _Init(data?: any) {
        this.choose.active = false;
        let type = GamerUI.Instance.type;
        let num = this.node.getSiblingIndex();
        if (data) {
            this.data = data;
            this.rare = data.rare;
            this.prefabUrl = data.prefab;
            this.nameLabel.string = data.name;
            this.pieceLabel.node.active = GamerUI.Instance.allSkin[type][num] != 1;
            this.pieceLabel.string = `${GamerUI.Instance.skinPieces[type][num]}/20`;
            this.choose.active = GamerUI.Instance.useSkins[type] == num;
            this.use.active = GamerUI.Instance.useSkins[type] == num;
            BundleManager.GetBundle("MainPack").load(data.spriteFrame, SpriteFrame, (err, data) => { this.itemSprite.spriteFrame = data; });
        }
        else {
            this.rare = 0;
            this.prefabUrl = null;
            this.nameLabel.string = "无";
            this.pieceLabel.node.active = false;
            this.choose.active = GamerUI.Instance.useSkins[type] == -1;
            this.use.active = GamerUI.Instance.useSkins[type] == -1;
            this.itemSprite.spriteFrame = this.emptySf;
        }
        this.rareSprite.spriteFrame = this.rareSfs[this.rare];
    }

    Click() {
        GamerUI.Instance.Choose(this.node);
        MainUI.Instance.PlayClick();
    }
}


